package com.beaut.project.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


/**
 * ExcelView
 * @author xf
 *
 */
public class ExcelView {
	
	@SuppressWarnings("unchecked")
	public static void buildExcelDocument(Map<String, Object> model,
			XSSFWorkbook wb,Sheet sheet, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
//		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		
		Map<String,String> infoMap = (Map<String, String>) model.get("infoMap");
		response.setHeader("Content-Disposition","attachment;filename="+new String(infoMap.get("fileName").getBytes(),"iso-8859-1")+".xlsx");
		response.setContentType("application/binary;charset=ISO8859_1");
		Map<String,String> tableTitleMap = (Map<String, String>) model.get("tableTitleMap");
		List<Map<Object,Object>> list = (List<Map<Object,Object>>)model.get("list");
		ExcelUtility.createHeader(tableTitleMap.size(),sheet,infoMap.get("title"),wb);
		ExcelUtility.createTableHeader(sheet,tableTitleMap,wb);
		ExcelUtility.shareExcelOut(sheet,wb,tableTitleMap,list);
		
	}
	@SuppressWarnings("unchecked")
	public static void buildXMExcelDocument(Map<String, Object> model,
			XSSFWorkbook wb,Sheet sheet, HttpServletRequest request, HttpServletResponse response)
					throws Exception {
		response.setHeader("Content-Disposition","attachment;filename="+new String("项目费用统计表".getBytes(),"iso-8859-1")+".xlsx");
		response.setContentType("application/binary;charset=ISO8859_1");
		List<Map<Object,Object>> list = (List<Map<Object,Object>>)model.get("list");
		ExcelUtility.buildXMExcelDocument(wb,sheet,list);
		
	}
}
